package cn.kai.ums.controller;

import cn.kai.ums.mapper.UmsClassMapper;
import cn.kai.ums.model.UmsClass;
import cn.kai.ums.service.UmsClassService;
import cn.kai.ums.tools.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ums/v1/class")
@Api(tags = "班级控制类")
public class ClassController {

    @Autowired
    private UmsClassService umsClassService;

    @Autowired
    private UmsClassMapper umsClassMapper;


    @ApiOperation("根据班级编号，班级名称，专业编号，进行模糊查询")
    @GetMapping
    public JsonData getClassList(@RequestParam(defaultValue = "1",required = false) Integer pageNum
            ,@RequestParam(defaultValue = "5",required = false) Integer pageSize
            ,@RequestParam(defaultValue = "",value = "likeclass",required = false) String likeclass){
        Page<UmsClass> umsClassPage = umsClassMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<UmsClass>lambdaQuery()
                        .like(UmsClass::getClassNo,likeclass)
                        .or()
                        .like(UmsClass::getClassName,likeclass)
                        .or()
                        .like(UmsClass::getClassSpecialNo,likeclass));
        return JsonData.buildSuccess(umsClassPage);
    }

    @PostMapping
    public JsonData saveClass(@RequestBody UmsClass umsClass){
       return JsonData.buildSuccess(umsClassService.save(umsClass));
    }


    @PutMapping
    public JsonData updateClass(@RequestBody UmsClass umsClass){
        return JsonData.buildSuccess(umsClassService.updateById(umsClass));
    }

    @DeleteMapping
    public JsonData delClass(@RequestParam Integer cid){
        return JsonData.buildSuccess(umsClassService.removeById(cid));
    }


    @PostMapping("/delBatch")
    public JsonData delBatch(@RequestBody List<Integer> cids){
        return JsonData.buildSuccess(umsClassService.removeByIds(cids));
    }
}
